<?php

if(!defined("INDEX"))
    die;

// ----------------------------------------------------------------------------

$smarty->assign('total_releases', $stats->getTotalReleases());
$smarty->assign('total_artists', $stats->getTotalArtists());
$smarty->assign('total_upcoming', $stats->getTotalUpcomingSchedules());
$smarty->assign('total_users', $stats->getTotalUsers());

$release_list = new ReleaseList();

$view = obtainHTTPVar('view', null);
$filter = obtainHTTPVar('filter', null);
$items = obtainHTTPVar('items', 20);

if($items > 500)
    $items = 500;

if(!in_array($view, array('new', 'upcoming', 'released')))
    $view = 'new';

$smarty->assign('view', $view);
$smarty->assign('items', $items);
$smarty->assign('filter', $filter);

switch($view) {
    case 'new':

        if(!empty($filter)) {

            try {
                $tag = new Tag;
                $tag->name = $filter;

                $smarty->assign('recent_schedules', $release_list->getRecentAddedSchedulesByTag($filter, $items,$user->id));
            } catch (Exception $e) {

                $smarty->assign('recent_schedules', $release_list->getRecentAddedSchedules($items,$user->id));
            }

        } else {
            
            $smarty->assign('recent_schedules', $release_list->getRecentAddedSchedules($items,$user->id));
        }
        
        break;

    case 'upcoming':

        if(!empty($filter)) {

            try {
                $tag = new Tag;
                $tag->name = $filter;

                $smarty->assign('upcoming_schedules', $release_list->getUpcomingSchedulesByTag($filter, $items,$user->id));
            } catch (Exception $e) {

                $smarty->assign('upcoming_schedules', $release_list->getUpcomingSchedules($items,$user->id));
            }

        } else {

            $smarty->assign('upcoming_schedules', $release_list->getUpcomingSchedules($items,$user->id));
        }
        break;

    case 'released':

        if(!empty($filter)) {

            try {
                $tag = new Tag;
                $tag->name = $filter;

                $smarty->assign('recent_releases', $release_list->getRecentReleasesByTag($filter, $items));
            } catch (Exception $e) {

                $smarty->assign('recent_releases', $release_list->getRecentReleases($items));
            }
        } else {
            $smarty->assign('recent_releases', $release_list->getRecentReleases($items));
        }
        break;
}

/* LASTFM RECOMMENDATIONS */
if(isset($_REQUEST['lastfm_login'])) {

    try {
        $lastfm_login = trim($_REQUEST['lastfm_login']);
        $lastfm_period = trim($_REQUEST['lastfm_period']);

        $smarty->assign('lastfm_login', $lastfm_login);
        $smarty->assign('lastfm_period', $lastfm_period);

        $lfm = new Lastfm;

        $user_artists = $lfm->getUserTopArtists($lastfm_login, $lastfm_period);

        $recommendation_ids = array();

        foreach($user_artists as $artist_name) {

            try {
                $artist = Artist::getByName($artist_name);
            } catch (Exception $e) {
                ; // do nothing
            }

            if($artist instanceof Artist) {

                $release_id = $artist->hasUpcomingRelease();

                if(!empty($release_id))
                    $recommendation_ids[] = $release_id;
            }
        }

        $smarty->assign('lastfm', $release_list->getReleasesByIds($recommendation_ids, 0, $user->id));
    } catch (Exception $e) {
        $smarty->assign('lastfm', null);
    }

}
